package com.smz.controller;
import com.smz.entity.PageEntity;
import com.smz.entity.TProductHandover;
import com.smz.service.TProductHandoverService;
import com.smz.service.TProductOrderService;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class TProductHandoverController {

    @Autowired
    private TProductHandoverService tProductHandoverService;

    @Autowired
    private TProductOrderService tProductOrderService;


    @ApiOperation("分页查询")
    @GetMapping("/ProductHandoverLike")
    public ResponseEntity<PageEntity<TProductHandover>> findPage(@Param("current") Integer current,
                                                                 @Param("pageSize") Integer pageSize,
                                                                 @Param("orderId") String orderId,
                                                                 @Param("time") String time){
        // 模糊查询到所有的订单
//        List<TProductOrder> productOrders = tProductOrderService.findProOrderLikeOrderId(orderId);
//        // 得到订单的主键id
//        List<Integer> orderIdList = new ArrayList<>();
//        for (TProductOrder productOrder : productOrders) {
//            orderIdList.add(productOrder.getId());
//        }
//        // 通过主键id
        if ("".equals(orderId)){
            orderId = null;
        }
        if ("".equals(time)){
            time = null;
        }
        System.out.println(current);
        PageEntity<TProductHandover> pageProHandover = tProductHandoverService.findPageProHandover(current,pageSize,orderId,time);
        return ResponseEntity.ok(pageProHandover);
    }

    // 添加交接单
    @PostMapping("ProductHandover")
    public ResponseEntity<String> addProHandover(@RequestBody TProductHandover tProductHandover){
        // 先判断是否已经添加过此表
        Integer orderId = tProductHandover.getOrderId();
        TProductHandover order = tProductHandoverService.findProHandoverByOrderId(orderId);
        if (order==null){
            tProductHandoverService.AddProHandover(tProductHandover);
            return ResponseEntity.ok("ok");
        }else {
            return ResponseEntity.ok("error");
        }
    }

    @DeleteMapping("ProductHandover/{id}")
    public ResponseEntity<String> addProHandover(@PathVariable("id") Integer id){
        tProductHandoverService.removeProHandoverById(id);
        return ResponseEntity.ok("ok");
    }

    @DeleteMapping("ProductHandovers")
    public ResponseEntity<String> addProHandover(@RequestBody String[] deleteIdArr){
        System.out.println(deleteIdArr);
        for (String deleteId : deleteIdArr) {
            int id = Integer.parseInt(deleteId);
            tProductHandoverService.removeProHandoverById(id);
        }
        return ResponseEntity.ok("ok");
    }


    // 修改交接单
    @PutMapping("ProductHandover")
    public ResponseEntity<String> updateProHandover(@RequestBody TProductHandover tProductHandover){
        tProductHandoverService.modifyProHandoverById(tProductHandover);
        return ResponseEntity.ok("ok");
    }


    // 修改状态
    @PutMapping("/state/{id}")
    public  ResponseEntity<String> updateState(@PathVariable("id") Integer id){
        tProductHandoverService.modifyProOrderState(id);
        return ResponseEntity.ok("ok");
    }

    /**
     * @Authoe: smz
     * @Description: 查询所有交接单
     * @Date: 16:43 2021-11-25
     */
    @GetMapping("/phoAndCs")
    public ResponseEntity<List<TProductHandover>> findAll(){
        List<TProductHandover> proHandovers = tProductHandoverService.findProHandovers();
        return ResponseEntity.ok(proHandovers);
    }

    /**
     * @Authoe: smz
     * @Description: 入库接口
     * @Date: 16:07 2021-11-27
     */
    @PostMapping("/tIoWareHouses/{menuId}/{reservoirId}/{ShelvesId}/{cargoId}/{ioNo}/{ioType}/{batch}/{ioTime}")
    public ResponseEntity<String> addTIoWareHouses(@PathVariable("menuId") String menuId,@PathVariable("reservoirId") String reservoirId,
                                                   @PathVariable("ShelvesId") String shelvesId,@PathVariable("cargoId") String cargoId,
                                                   @PathVariable("ioNo") String ioNo,@PathVariable("batch") String batch,
                                                   @PathVariable("ioType") String ioType,@PathVariable("ioTime") String ioTime,
                                                   @RequestBody TProductHandover tProductHandover){
        tProductHandoverService.addAndUpdateTIoWareHouse(tProductHandover,menuId,reservoirId,shelvesId,cargoId,ioNo,batch,ioTime,ioType);
        return ResponseEntity.ok("ok");
    }
}
